//
//  SLSelectCourseViewController.m
//  StarLight
//
//  Created by Apple on 16/8/25.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

// 说明: 添加路线控制器

#import "SLSelectCourseViewController.h"
#import "SLSelectWatercraftViewCell.h"
#import "SLDeployModel.h"

@interface SLSelectCourseViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;
@property (weak, nonatomic) IBOutlet UIButton *beginBtn; // 出发地按钮
@property (weak, nonatomic) IBOutlet UIButton *endBtn;  // 目的地按钮

@property (nonatomic, strong) NSArray *titleArr;

@end


static NSString * const watercraftID = @"SLSelectWatercraftViewCell";
@implementation SLSelectCourseViewController


#pragma mark -- 懒加载代码
- (NSArray *)titleArr
{
    if (_titleArr == nil) {
        
        //1.拿到 mainBundle
        NSBundle *myMainBundle = [NSBundle mainBundle];
        //2.根据mainBundle 获取路径
        NSString *filePath = [myMainBundle pathForResource:@"SLArea" ofType:@"plist"];
        
        //3.直接创建数组
        _titleArr = [NSArray arrayWithContentsOfFile:filePath];
        
    }
    return _titleArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title = @"添加路线";
    // 显示导航条
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    
    self.rightTableView.dataSource = self;
    self.rightTableView.delegate = self;
    
    // 注册cell
    [self.leftTableView registerNib:[UINib nibWithNibName:@"SLSelectWatercraftViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:watercraftID];
    
    // 注册cell
    [self.rightTableView registerNib:[UINib nibWithNibName:@"SLSelectWatercraftViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:watercraftID];
    
    [self.beginBtn setTitle:[NSString stringWithFormat:@"出发地"] forState:UIControlStateNormal];
    
    [self.beginBtn setImage:[UIImage imageNamed:@"v"] forState:UIControlStateNormal];
    
    [self.endBtn setTitle:[NSString stringWithFormat:@"目的地"] forState:UIControlStateNormal];
    
    [self.endBtn setImage:[UIImage imageNamed:@"v"] forState:UIControlStateNormal];
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    // 控制器View即将消失的时候隐藏导航条
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}


- (IBAction)departLandBtn {
    
    self.leftTableView.hidden = NO;
    self.rightTableView.hidden = YES;
}


- (IBAction)destinationBtn {
    
    self.leftTableView.hidden = YES;
    self.rightTableView.hidden = NO;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SLSelectWatercraftViewCell *cell = [tableView dequeueReusableCellWithIdentifier:watercraftID];
    
    [cell.contentLabel setText:[NSString stringWithFormat:@"%@", _titleArr[indexPath.row]]];
    
    cell.textLabel.highlightedTextColor = [UIColor yellowColor];
    
    return cell;
}

#pragma mark -- <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.leftTableView.hidden = YES;
    self.rightTableView.hidden = YES;
  
    if (tableView == _leftTableView) {
        
        [self.beginBtn setTitle:[NSString stringWithFormat:@"%@", _titleArr[indexPath.row]] forState:UIControlStateNormal];
        
        [self.beginBtn setImage:[UIImage imageNamed:@"v"] forState:UIControlStateNormal];
    }
    
    if (tableView == _rightTableView) {
        
        [self.endBtn setTitle:[NSString stringWithFormat:@"%@", _titleArr[indexPath.row]] forState:UIControlStateNormal];
        
        [self.endBtn setImage:[UIImage imageNamed:@"v"] forState:UIControlStateNormal];
        
        SLDeployModel *model = [SLDeployModel initWithBeginStr:self.beginBtn.titleLabel.text endStr:self.endBtn.titleLabel.text];
        
        if ([self.delegate respondsToSelector:@selector(SLSelectCourseViewController:didSLDeployModel:)]) {
            
            [self.delegate SLSelectCourseViewController:self didSLDeployModel:model];
        };
        
        [self.navigationController popViewControllerAnimated:YES];
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}


@end
